/**
 * Created by dosir on 2017/5/4.
 * 消息js包含图表
 */
define(function(require,exports){
    var common = require("common");

    exports.init=function(){

        //编辑短信条数
        $(".sms_info").on("click","[data-act='editRisk']",function(){
           var $input = $(this).prev(".risk_input");
           var active = $(this).hasClass("active");
           if(active){
               var val = $input.val();
               var reg = /^[1-9]\d*$/;
               var test = reg.test(val);
               if(!test) {
                   layer.alert("短信预警数必须是大于0的整数！！");
                   return false;
               }

               $(this).removeClass("active").text("编辑");
               $input.addClass("readonly").prop("readonly",true);

               common.ajaxHandle({
                   url:"/admin/setting/do_edit",
                   data:{"configs[sms_risk]":val},
                   isLoading: true
               })
           }else {
               $(this).addClass("active").text("保存");
               $input.removeClass("readonly").prop("readonly",false);
           }
        });
        //解绑
        $(".sms_info").on("click","[data-act='unbind']",function(){
            common.ajaxHandle({
                url:"/admin/setting/do_edit",
                data:{"configs[sms_risk]":"","configs[sms_open]":"","configs[sms_account]":"","configs[sms_password]":"","configs[sms_sign]":""},
                isLoading: true
            })
        });

        /*=========================图表================================*/
        //查找当前颜色
        var skin = '.' + $("#changeSkin").data("now"), current_color;
        var $skin = $("#changeSkin").find(skin);
        if ($skin.length > 0) {
            current_color = $skin.data("color");
        } else {
            current_color = $("#changeSkin").find(".skin").data("color");
        }
        //计算短信超出预警百分比
        var d1 =0,d2=0;
        (function(){
            var sms_rest = Number($("#smsRest").html());
            var sms_risk = Number($("#smsRisk").html());
            if(sms_risk-sms_rest<=0) {
                d2 = sms_risk;
            }else {
                d1 = sms_risk-sms_rest;
                d2 = sms_rest;

            }
        })();
        console.log(d1,d2)

        //饼状图
        var myChart = echarts.init(document.getElementById('chartSms'));
        var option = {
            series: [
                {
                    type: 'pie',
                    center: ['50%', '50%'],
                    radius: [50, 55],
                    //roseType:"radius",
                    //x: '0%', // for funnel
                    itemStyle: {
                        normal: {
                            color: current_color,
                            label: {
                                formatter:"{d}%"
                            }
                        },
                    },
                    data: [
                        {name: '短信预警数', value: d1,
                            itemStyle:{
                                normal: {
                                    label: {
                                        show: true,
                                        position: 'center',
                                        textStyle: {
                                            color: current_color,
                                            baseline: 'bottom',
                                            fontSize: "22"

                                        }
                                    },
                                    labelLine: {
                                        show: false
                                    }
                                }
                            }
                        },
                        {name: '超出预警数量', value: d2,
                            itemStyle:{
                                normal: {
                                    color: '#ccc',
                                    label: {
                                        show: true,
                                        position: 'center',
                                        formatter: '{b}',
                                        textStyle: {
                                            color: "#888",
                                            fontSize: "14",
                                            baseline: 'top',
                                            //fontSize: "px"
                                        }
                                    },
                                    labelLine: {
                                        show: false
                                    }
                                }
                            }}
                    ]
                },
            ]
        };

        // 为echarts对象加载数据
        myChart.setOption(option);
    };

});